package com.ukao.cashregister.utils;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestListener;
import com.ukao.cashregister.R;

/**
 * Created by sunfusheng on 16/4/6.
 */
public class ImageUtils {


    public static final String ANDROID_RESOURCE = "android.resource://";
    public static final String FOREWARD_SLASH = "/";


    // 将资源ID转为Uri
    public static Uri resourceIdToUri(Context mContext, int resourceId) {
        return Uri.parse(ANDROID_RESOURCE + mContext.getPackageName() + FOREWARD_SLASH + resourceId);
    }


    /**
     * 加载drawable图片 是否显示默认图片
     *

     */
    public static void loadResImage(Context mContext, int resId, ImageView imageView  ) {
            Glide.with(mContext)
                    .load(resourceIdToUri(mContext, resId))
                    .crossFade()
                    .into(imageView);


    }


    /**
     * 加载本地或者网络资源
     *
     * @param mContext
     * @param path
     * @param imageView
     */
    public static void loadImage(Context mContext, String path, ImageView imageView) {

        if (!CheckUtils.isEmpty(path)&&path.length()>4 && !path.contains("http")) {
            path = "file://" + path;

        }

        /*   .placeholder(R.drawable.load_image)
                    .error(R.drawable.load_image)*/
        if (!CheckUtils.isEmpty(path) ){
            Glide.with(mContext)
                    .load(path)
                    .crossFade()
                    .placeholder(R.drawable.load_image)
                    .error(R.drawable.load_image)
                    .into(imageView);


        } else{
            Glide.with(mContext)
                    .load(resourceIdToUri(mContext, R.drawable.load_image))
                    .placeholder(R.drawable.load_image)
                    .error(R.drawable.load_image)
                    .crossFade()
                    .into(imageView);
        }




    }

    /**
     *加载默认图片
     */
    public static void loadImage(Context mContext, String path, ImageView imageView,int defaultImage) {

        if (!CheckUtils.isEmpty(path)&&path.length()>4  && path.contains("http") ){
            Glide.with(mContext)
                    .load(path)
                    .crossFade()
                    .placeholder(defaultImage)
                    .error(defaultImage)
                    .into(imageView);


        } else{
            Glide.with(mContext)
                    .load(resourceIdToUri(mContext,defaultImage))
                    .crossFade()
                    .into(imageView);
        }




    }



    /**
     * @param mContext
     * @param path
     * @param imageView
     * @param mRequestListener 加载图片回调
     */

    public static void loadImage(Context mContext, String path, ImageView imageView, RequestListener mRequestListener) {
        if (!CheckUtils.isEmpty(path)&&path.length()>4  && path.contains("http") ){
            Glide.with(mContext)
                    .load(path)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .listener(mRequestListener)
                    .crossFade()
                    .into(imageView);
        }
    }



    public static void loadImages(Context mContext, String path, ImageView imageView) {

        if (!CheckUtils.isEmpty(path)&&path.length()>4 && !path.substring(0, 4).equals("http")) {
            path = "file://" + path;

        }

        try {
            if (!CheckUtils.isEmpty(path) ){
                Glide.with(mContext)
                        .load(path)
                        .placeholder(R.drawable.blue_color)
                        .error(R.drawable.blue_color)
                        .crossFade()
                        .into(imageView);


            }

        }catch (Exception  e){



        }




    }


    /**
     *设置比列
     */
    public static void setViewWith(Context mContext, View mView, int distance, float num ) {
        ViewGroup.LayoutParams params = mView.getLayoutParams();
        params.width = (int) ((DensityUtils.getScreenW(mContext) - DensityUtils.dip2px(mContext, distance)) *num);
        mView.setLayoutParams(params);


    }
    /**
     *设置图片比列
     */
    public static void setViewWithoter(Context mContext, View mImageView, int distance, int num) {
        ViewGroup.LayoutParams params = mImageView.getLayoutParams();
        params.width = (DensityUtils.getScreenW(mContext) - DensityUtils.dip2px(mContext,distance)) / num;
        params.height = (int) (params.width*0.65);
        mImageView.setLayoutParams(params);



    }







}